package com.zhq.blog.core.domain.product;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.zhq.blog.core.domain.BasePersistentDomain;
import com.zhq.blog.core.domain.search.NoteSearch;

@Entity
public class Note extends BasePersistentDomain<NoteRepository> {
	
	private String name;     //奖品名称
	private String userName; //用户名字
	private long gold;		//金币
	private long nums;  //兑奖数量
	
	
	public Note(){}
	
	
	
	public Note(String name, String userName, long gold, long nums) {
		super();
		this.name = name;
		this.userName = userName;
		this.gold = gold;
		this.nums = nums;
	}

	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}



	public String getUserName() {
		return userName;
	}



	public void setUserName(String userName) {
		this.userName = userName;
	}



	public long getGold() {
		return gold;
	}



	public void setGold(long gold) {
		this.gold = gold;
	}



	public long getNums() {
		return nums;
	}



	public void setNums(long nums) {
		this.nums = nums;
	}



	public  Specification<Note> selectCondition(final NoteSearch noteSearch) {
		return new Specification<Note>() {
			@Override
			public Predicate toPredicate(Root<Note> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {

				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				
				expressions.add(builder.equal(root.<Boolean> get("deleted"),0));

				if (StringUtils.isNotBlank(noteSearch.getName())) {
					expressions.add(builder.or( builder.like(root.<String> get("name"), "%"+ noteSearch.getName() + "%")));

					
				}
				if(StringUtils.isNotBlank(noteSearch.getNoteStartDate()) && StringUtils.isBlank(noteSearch.getNoteEndDate())){
					Date star = null;
					try {
						star = sdf.parse(noteSearch.getNoteStartDate());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					expressions.add(builder.greaterThanOrEqualTo(root.get("createdDate"),star));
				}
				
				if(StringUtils.isBlank(noteSearch.getNoteStartDate())&& StringUtils.isNotBlank(noteSearch.getNoteEndDate())){
					Date end = null;
					try {
						end = sdf.parse(noteSearch.getNoteEndDate());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					expressions.add(builder.greaterThanOrEqualTo(root.get("createdDate"), end));
				}

				if(StringUtils.isNotBlank(noteSearch.getNoteStartDate()) && StringUtils.isNotBlank(noteSearch.getNoteEndDate())){
					Date end = null;
					Date star = null;
					try {
						star = sdf.parse(noteSearch.getNoteStartDate());
						end = sdf.parse(noteSearch.getNoteEndDate());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					expressions.add(builder.between(root.get("createdDate"), star,end)); // 创建时间createdTime
				}

				return query.where(predicate).getRestriction();

			}
		};
	}
	
	
	
}
